package com.appbee.gdata.picasa.server;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;

import com.appbee.settings.server.Setting;
import com.appbee.settings.server.SettingsDAO;

@Singleton
public class PicasaUserAccount
{
	private Setting userSetting;
	private Setting pwSetting;
	private final Provider<SettingsDAO> settingsDAOProvider;
	private boolean loaded = false;
	private boolean connected = false;

	@Inject
	public PicasaUserAccount(Provider<SettingsDAO> settingsDAOProvider)
	{
		super();
		this.settingsDAOProvider = settingsDAOProvider;
	}

	public Setting getUserSetting()
	{
		if (userSetting == null)
		{
			userSetting = new Setting();
			userSetting.setId("picasa.user");
		}
		return userSetting;
	}

	private Setting getPwSetting()
	{
		if (pwSetting == null)
		{
			pwSetting = new Setting();
			pwSetting.setId("picasa.password");
		}
		return pwSetting;
	}

	public String getUserName()
	{
		return getUserSetting().getValue();
	}

	public void setUserName(String userName)
	{
		getUserSetting().setValue(userName);
	}

	public void setPassword(String password)
	{
		getPwSetting().setValue(password);
	}

	public boolean isConnected()
	{
		return connected;
	}

	public void setConnected(boolean connected)
	{
		this.connected = connected;
	}

	public void ensureLoaded()
	{
		if (!loaded)
		{
			load();
			loaded = true;
		}
	}

	public void load()
	{
		userSetting = settingsDAOProvider.get().get("picasa.user");
		pwSetting = settingsDAOProvider.get().get("picasa.password");
		connected = false;
	}

	public synchronized void save()
	{
		settingsDAOProvider.get().save(getUserSetting());
		settingsDAOProvider.get().save(getPwSetting());
		connected = false;
	}

	public String getPassword()
	{
		return getPwSetting().getValue();
	}

}
